package com.lc.controller;

import com.lc.mapper.AreaMapper;
import com.lc.mapper.CarMapper;
import com.lc.mapper.ShopMapper;
import com.lc.pojo.Area;
import com.lc.pojo.Car;
import com.lc.pojo.Shop;
import com.lc.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/car")
@Slf4j
public class CarController {

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CarService carService;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private ShopMapper shopMapper;

    // 车辆首页（查询页）
    @RequestMapping("/city")
    public String shopCityList(Model model){

        List<Area> areas = areaMapper.queryCityList();
        model.addAttribute("areas", areas);
        return "shop/carSelect";
    }

    // 根据城市名查询地区名
    @ResponseBody
    @RequestMapping("/area")
    public List<String> shopAreaList(String city){
        return areaMapper.queryAreaList(city);
    }

    // 根据城市名&地区名查询门店名
    @ResponseBody
    @RequestMapping("/shopName")
    public List<String> shopNameList(String city, String area){
        return shopMapper.queryShopName(city,area);
    }

    // 根据城市，地区，门店名返回车辆列表
    @RequestMapping("/toList")
    public String toShopList(HttpServletRequest request, Shop shop, Model model) throws Exception {
        List<Car> cars= carMapper.queryCarListByShop(shop.getShop_name());
        model.addAttribute("cars", cars);
        return "mange/car";

    }

    // 点击修改按钮，跳转至修改页面
    @RequestMapping("/toEdit/{car_number}")
    public String toAdd(@PathVariable String car_number, Model model) throws Exception {
        Car car = carMapper.queryCarByNum(car_number);
        model.addAttribute("car", car);
        return "mange/edit";
    }

    // 提交车辆修改，跳转至车辆列表
    @RequestMapping("/update")
    public String updateCar(HttpServletRequest request, Car car, Model model) throws Exception {
        Car carNew = new Car();
        BeanUtils.copyProperties(car,carNew);
        Boolean succ = carService.updateCar(carNew);
        if (succ) {
            String successMsg = URLEncoder.encode("修改成功", "UTF-8");
            List<Car> cars= carMapper.queryCarListByShop(car.getShop_name());
            model.addAttribute("cars", cars);
            return "mange/car";
            // return "redirect:/car/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("修改失败", "UTF-8");
            return "redirect:/car/list?successMsg=" + errorMsg;
        }
    }

    // 点击新增车辆按钮，跳转至新增页面
    @RequestMapping("/toAdd")
    public String toAdd() throws Exception {
        return "mange/add";

    }

    // 提交车辆新增，跳转至车辆列表
    @RequestMapping("/add")
    public String addCar(HttpServletRequest request, Car car, Model model) throws Exception {
        Car carNew = new Car();
        BeanUtils.copyProperties(car,carNew);
        Boolean succ = carService.addCar(carNew);
        if (succ) {
            String successMsg = URLEncoder.encode("增加成功", "UTF-8");
            List<Car> cars= carMapper.queryCarListByShop(car.getShop_name());
            model.addAttribute("cars", cars);
            return "mange/car";
            // return "redirect:/car/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("增加失败", "UTF-8");
            return "redirect:/car/list?successMsg=" + errorMsg;
        }
    }

    // 删除车辆
    @RequestMapping("/delete/{car_number}")
    public String deleteCar(@PathVariable String car_number, Model model) throws Exception {
        String shopName = carMapper.queryShopNameByCarId(car_number);

        int succ = carMapper.deleteCarByNum(car_number);
        List<Car> cars= carMapper.queryCarListByShop(shopName);
        if (succ==1) {
            String successMsg = URLEncoder.encode("删除成功", "UTF-8");
            model.addAttribute("cars", cars);
            return "mange/car";
            //return "redirect:/car/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("删除失败", "UTF-8");
            return "redirect:/car/list?successMsg=" + errorMsg;
        }
    }

}
